$(function(){
    var code = $("#tutor_code").val();
    $.getJSON('/tts/tutor/json/'+code,function(data){
        data.contents = _.groupBy(data.contents,function(o){
            return o.theme.code+","+o.theme.name;
        })
        app.teacher = data;
        app.initKeys();
    });
    $.getJSON('/tts/themes/json/'+code,function(data){
        app.themes = data;
    })
});
// console.log(teacher);



var app = new Vue({
  el:'#app',
  data: {
    teacher: {},
    showItems:[],
    showAdd:[],
    newItems: [],
    errorMsgs:[],
    keys:[],
    themes:[],
    itemSaveUrl : "/tts/tutor/theme-list/save",
    rules:{
      title:[{required:true,message:'请输入项目标题',trigger:'blur'}],
      content:[{required:true,message:'请输入项目内容',trigger:'blur'}],
    },
      rules2:{
          title:[{required:true,message:'请输入项目标题',trigger:'blur'}],
          content:[{required:true,message:'请输入项目内容',trigger:'blur'}],
      },
      editFormTitle:"编辑",
      editFormVisible:false,
      btnEditText:'提交',
      editForm: {
        id:null,
        title:'',
        content:'',
        rank: 0
      },
      selections:[]

  },
  created:function(){
    // console.log(this.teacher.contents)
  },
  watch:{
  },
  computed:{
      imagePath:function(){
          return "https://hr.sufe.edu.cn/hr/upload/photo/"+this.teacher.headShot;
      },
  },
  methods: {
      handleEdit:function(row){
          this.editFormVisible=true;
          this.editForm = row;
      },
      handleDel:function(row){
          console.log(row)
      },
      editSubmit:function(){
          var _this = this;
          _this.$refs.editForm.validate(function(valid){
              if(valid){
                  // do ajax submit
                  _this.ajaxSubmit(_this.itemSaveUrl,getJSONArrayFromObject(_this.editForm,"item"));
                  _this.editFormVisible = false;
              }
          });
      },
      handleSelectionChange:function(index,val) {
          this.selections[index] = val;
        },
      show:function(index){
          var data = [];
          data.push({name:'type',value:'2'});
        for(var i=0;i<this.selections[index].length;i++){
            // var item = this.selections[index][i].
            this.selections[index][i].isOpen = true;
            data.push({"name":"ids",value:this.selections[index][i].id});
            // to another ajax to speed up
        }
          this.ajaxSubmit("/tts/tutor/batchUpdate",data);
      },
      hidden:function(index){
          var data = [];
          data.push({name:'type',value:'1'});
          for(var i=0;i<this.selections[index].length;i++){
              // var item = this.selections[index][i].
              this.selections[index][i].isOpen = false;
              data.push({"name":"ids",value:this.selections[index][i].id});
              // to another ajax to speed up
          }
          this.ajaxSubmit("/tts/tutor/batchUpdate",data);
      },
      up:function(index){
          var data = [];
          data.push({name:'type',value:'0'});
          for(var i=0;i<this.selections[index].length;i++){
              // var item = this.selections[index][i].
              this.selections[index][i].rank = 0;
              // to another ajax to speed up
              data.push({"name":"ids",value:this.selections[index][i].id});
          }
          this.ajaxSubmit("/tts/tutor/batchUpdate",data);
      },
      sortByRank:function(items){
          return items.sort(function(a,b){
              return a.rank-b.rank;
          })
      },
      formatter:function(row,column){
          if(row.isOpen){
              return "可见";
          }else{
              return "隐藏";
          }
      },
    initKeys:function(){
        this.keys=_.keys(this.teacher.contents);
        var len = this.keys.length;
        this.showItems.length= len;
        this.showAdd.length = len;
        this.newItems.length = len;
        this.errorMsgs.length = len;
        _.fill(this.showItems,true);
        _.fill(this.showAdd,false);
        _.fill(this.newItems,{'title':'', 'content':'', 'isOpen':true, 'status':true, rank:0});
        _.fill(this.errorMsgs,'errorMsg')
    },
      resort:function (index) {
        this.teacher.contents[this.keys[index]].sort(function(a,b){
            return a.rank -b.rank;
        })
      },
    showAddForm:function(index){
      Vue.set(app.showAdd,index,true);
      Vue.set(app.showItems,index,false);
    },
      ajaxSubmit:function(url,json){
        var $this = this;
         $.post(url,json)
             .done(function(data){
                 $this.$message({
                     message:'恭喜，信息添加成功',
                     type:'success'
                 });
             }).fail(function(data){
                 $this.$message({
                     message:'信息验证失败，请填写正确的信息',
                     type:'error',
                     showClose:true,
                     duration:3000
                 });
             });
      },
      switchRow:function(row){
          this.ajaxSubmit(this.itemSaveUrl,getJSONArrayFromObject(row,"item"));
      },
    saveForm:function(index){
      var $this = this;
      this.$refs['ruleForm'][index].validate(function(valid){
        if(valid){
          var item = _.cloneDeep($this.newItems[index])
          item.theme = $this.themes[index];

          $this.ajaxSubmit($this.itemSaveUrl,getJSONArrayFromObject(item,"item"));
          $this.teacher.contents[$this.keys[index]].splice(0,0,item);
          $this.cancelForm(index)
        }else {
          $this.$message({
            message:'信息验证失败，请填写正确的信息',
            type:'error',
            showClose:true,
            duration:3000
          });
          console.log("error submit");
          return false;
        }
      });
    },
    cancelForm:function(index) {
        this.$refs['ruleForm'][index].resetFields();
      Vue.set(app.newItems,index,{'title':'', 'content':'', 'isOpen':true, 'status':true, rank:0});
      Vue.set(app.showAdd,index,false);
      Vue.set(app.showItems,index,true);
    },
    modifyphoto:function(){
        window.open("http://portal.sufe.edu.cn/web/guest/appcenter?p_p_id=jigsawAppFront_WAR_jigsawAppFrontportlet&p_p_lifecycle=0&p_p_state=pop_up&p_p_mode=view&_jigsawAppFront_WAR_jigsawAppFrontportlet_action=linkurl&_jigsawAppFront_WAR_jigsawAppFrontportlet_appid=46389");
    },
    changeOpen:function(){
        var data = [];
        data.push({name:'id',value:app.teacher.id});
        this.ajaxSubmit("/tts/tutor/toggle",data);
    }
  }
})
